From d2f16be3a27bd590d1b8d09e9249dca002c8ef77 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Sun, 16 Apr 2023 09:32:46 -0600 Subject: [PATCH] use c++ style attribute syntax with gnu namespace. (#1069) * use c++ style attribute syntax with gnu namespace. cppcheck 2.10.3 seems to have difficulty combining c++ style attributes like [[noreturn]] with gnu __attribute__ syntax. this leads to lots of false positive [nullPointerRedundantCheck] warings. I have a feeling I did this before and it failed somewhere. Perhaps our compilers are all ready for it now. * fix 7 real nullPointerRedundantCheck warnings. and 7 nullPointerArithmeticRedundantCheck warnings 1 redundantInitialization warning 1 unreadVariable warning * fix 1 nullPointerRedundantCheck. A more involved fix would be to pass a reference to a non-const parameter to mkshort_del_handle to be used as an in-out parameter. * fix 1 nullPointerRedundantCheck * workaround cppcheck bug with noretun. --- defs.h | 27 +++++++++++++-------------- garmin_gpi.cc | 2 +- mkshort.cc | 6 +++++- util.cc | 35 ++++++++++++++++------------------- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/defs.h b/defs.h index 35fa3d9a2..c4b68a2e0 100644 --- a/defs.h +++ b/defs.h @@ -129,15 +129,6 @@ constexpr int kDautmWGS84 = 118; // GPS_Lookup_Datum_Index("WGS 84") # define GB_PATHSEP '/' #endif -/* - * Toss in some GNU C-specific voodoo for checking. - */ -#if __GNUC__ -# define PRINTFLIKE(x,y) __attribute__ ((__format__ (__printf__, (x), (y)))) -#else -# define PRINTFLIKE(x,y) -#endif - /* * Common definitions. There should be no protocol or file-specific @@ -984,8 +975,16 @@ struct ff_vecs_t { }; [[noreturn]] void fatal(QDebug& msginstance); -[[noreturn]] void fatal(const char*, ...) PRINTFLIKE(1, 2); -void warning(const char*, ...) PRINTFLIKE(1, 2); +// cppcheck 2.10.3 fails to assign noreturn attribute to fatal if +// the noreturn attribute is listed before the gnu::format attribute. +// A PR to resolve this is https://github.com/danmar/cppcheck/pull/4971, +// but cppcheck works if the noreturn attribute follows the gnu::format +// attribute. +// This can have a large effect on codacy issues from cppcheck +// nullPointerRedundantCheck, nullPointerArithmeticRedundantCheck, +// negativeIndex, arrayIndexOutOfBoundsCond. +[[gnu::format(printf, 1, 2)]] [[noreturn]] void fatal(const char*, ...); +[[gnu::format(printf, 1, 2)]] void warning(const char*, ...); void printposn(double c, bool is_lat); @@ -1018,9 +1017,9 @@ inline int case_ignore_strncmp(const QString& s1, const QString& s2, int n) int str_match(const char* str, const char* match); -int xasprintf(char** strp, const char* fmt, ...) PRINTFLIKE(2, 3); -int xasprintf(QString* strp, const char* fmt, ...) PRINTFLIKE(2, 3); -int xasprintf(QScopedPointer& strp, const char* fmt, ...) PRINTFLIKE(2, 3); +[[gnu::format(printf, 2, 3)]] int xasprintf(char** strp, const char* fmt, ...); +[[gnu::format(printf, 2, 3)]] int xasprintf(QString* strp, const char* fmt, ...); +[[gnu::format(printf, 2, 3)]] int xasprintf(QScopedPointer& strp, const char* fmt, ...); int xvasprintf(char** strp, const char* fmt, va_list ap); char* strupper(char* src); char* strlower(char* src); diff --git a/garmin_gpi.cc b/garmin_gpi.cc index 9a14f2332..4ec4661ca 100644 --- a/garmin_gpi.cc +++ b/garmin_gpi.cc @@ -71,10 +71,10 @@ garmin_fs_t* GarminGPIFormat::gpi_gmsd_init(Waypoint* wpt) { - garmin_fs_t* gmsd = garmin_fs_t::find(wpt); if (wpt == nullptr) { fatal(MYNAME ": Error in file structure.\n"); } + garmin_fs_t* gmsd = garmin_fs_t::find(wpt); if (gmsd == nullptr) { gmsd = garmin_fs_alloc(-1); wpt->fs.FsChainAdd(gmsd); diff --git a/mkshort.cc b/mkshort.cc index c343b2519..e7e4fe88b 100644 --- a/mkshort.cc +++ b/mkshort.cc @@ -161,9 +161,13 @@ mkshort_add_to_list(mkshort_handle_imp* h, char* name) void mkshort_del_handle(short_handle* h) { + if (!h) { + return; + } + auto* hdr = (mkshort_handle_imp*) *h; - if (!h || !hdr) { + if (!hdr) { return; } diff --git a/util.cc b/util.cc index a650fa36d..ca7b2a4a4 100644 --- a/util.cc +++ b/util.cc @@ -1061,14 +1061,11 @@ pretty_deg_format(double lat, double lon, char fmt, const char* sep, bool html) QString strip_nastyhtml(const QString& in) { - char* returnstr; - char* lcstr; + char* returnstr = xstrdup(in); + char* lcstr = strlower(xstrdup(in)); - char* sp = returnstr = xstrdup(in); - char* lcp = lcstr = strlower(xstrdup(in)); - - while (lcp = strstr(lcstr, ""), nullptr != lcp) { - sp = returnstr + (lcp - lcstr) ; /* becomes */ + while (char* lcp = strstr(lcstr, "")) { + char* sp = returnstr + (lcp - lcstr) ; /* becomes */ sp++; *sp++ = '!'; *sp++ = ' '; @@ -1076,8 +1073,8 @@ strip_nastyhtml(const QString& in) *sp++ = ' '; *lcp = '*'; /* so we wont find it again */ } - while (lcp = strstr(lcstr, " */ - sp = returnstr + (lcp - lcstr) ; + while (char* lcp = strstr(lcstr, " */ + char* sp = returnstr + (lcp - lcstr) ; sp++; *sp++ = '!'; *sp++ = '-'; @@ -1089,8 +1086,8 @@ strip_nastyhtml(const QString& in) *--sp = '-'; *lcp = '*'; /* so we wont find it again */ } - while (lcp = strstr(lcstr, ""), nullptr != lcp) { - sp = returnstr + (lcp - lcstr) ; /* becomes */ + while (char* lcp = strstr(lcstr, "")) { + char* sp = returnstr + (lcp - lcstr) ; /* becomes --> */ *sp++ = ' '; *sp++ = ' '; *sp++ = ' '; @@ -1131,8 +1128,8 @@ strip_nastyhtml(const QString& in) *sp++ = '-'; *lcp = '*'; /* so we wont find it again */ } - while (lcp = strstr(lcstr, "